package com.example.demo.entryWarehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.common.ResponseData;
import com.example.demo.entryWarehouse.dto.AcceptGood;
import com.example.demo.entryWarehouse.entity.AcceptGoods;
import com.example.demo.entryWarehouse.entity.AcceptInform;
import com.example.demo.entryWarehouse.mapper.AcceptGoodsMapper;
import com.example.demo.entryWarehouse.mapper.AcceptInformMapper;
import com.example.demo.entryWarehouse.service.AcceptGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangxuan
 * @since 2021-05-03
 */
@Service
public class AcceptGoodsServiceImpl extends ServiceImpl<AcceptGoodsMapper, AcceptGoods> implements AcceptGoodsService {

    @Autowired
    private AcceptGoodsMapper acceptGoodsMapper;

    @Autowired
    private AcceptInformMapper acceptInformMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData addGood(AcceptGood acceptGood) {
        AcceptGoods acceptGoods = new AcceptGoods();
        acceptGoods.setAcceptId(acceptGood.getAcceptId());
        acceptGoods.setName(acceptGood.getName());
        acceptGoods.setSpecification(acceptGood.getSpecification());
        acceptGoods.setUnit(acceptGood.getUnit());
        acceptGoods.setType(acceptGood.getType());
//        acceptGoods.setBillNum(0);
//        acceptGoods.setYesNum(0);
//        acceptGoods.setPrice(BigDecimal.ZERO);
//        acceptGoods.setTax(BigDecimal.ZERO);
//        acceptGoods.setYesNum(0);
//        acceptGoods.setNoNum(0);
//        acceptGoods.setShortage(0);
//        acceptGoods.setSurplus(0);
        QueryWrapper<AcceptGoods> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("accept_id",acceptGood.getAcceptId())
                .eq("name",acceptGood.getName())
                .eq("specification",acceptGood.getSpecification())
                .eq("unit",acceptGood.getUnit());
        AcceptGoods acceptGoods1 = new AcceptGoods();
        acceptGoods1 = acceptGoodsMapper.selectOne(wrapper1);
        if (acceptGoods1 == null){
            //某次验收且第一次验收该物品
            if (acceptGood.getYesorno().equals("合格")){
                acceptGoods.setYesNum(1);
                acceptGoods.setNoNum(0);
            }else {
                acceptGoods.setYesNum(0);
                acceptGoods.setNoNum(1);
            }
            int i = acceptGoodsMapper.insert(acceptGoods);
            if (i > 0) return ResponseData.buildOk();
            return ResponseData.buildError();
        }else {
            if (acceptGood.getYesorno().equals("合格")){
                int num = acceptGoods1.getYesNum() + 1;
                UpdateWrapper<AcceptGoods> wrapper2 = new UpdateWrapper<>();
                wrapper2.eq("accept_id",acceptGood.getAcceptId())
                        .eq("name",acceptGood.getName())
                        .eq("specification",acceptGood.getSpecification())
                        .eq("unit",acceptGood.getUnit())
                        .set("yes_num", num);
                int j = acceptGoodsMapper.update(null,wrapper2);
                if (j > 0) return ResponseData.buildOk();
                return ResponseData.buildError();
            }else {
                int num = acceptGoods1.getNoNum() + 1;
                UpdateWrapper<AcceptGoods> wrapper2 = new UpdateWrapper<>();
                wrapper2.eq("accept_id",acceptGood.getAcceptId())
                        .eq("name",acceptGood.getName())
                        .eq("specification",acceptGood.getSpecification())
                        .eq("unit",acceptGood.getUnit())
                        .set("yes_num", num);
                int j = acceptGoodsMapper.update(null,wrapper2);
                if (j > 0) return ResponseData.buildOk();
                return ResponseData.buildError();
            }
        }

    }

    @Override
    public ResponseData editGood(AcceptGoods acceptGoods) {
        //此处注意，需要测试,主要是bigdemical转json！！！undo
        //只更新账单所列数量、*单价、*税率、合格数量、不合格数量、存放位置、备注
        int i = acceptGoodsMapper.updateById(acceptGoods);
        if (i > 0) return ResponseData.buildOk();
        return ResponseData.buildError();
    }

    @Override
    public ResponseData selectById(String acceptId) {
        List<AcceptGoods> acceptGoodsList = new ArrayList<>();
        QueryWrapper<AcceptGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("accept_id",acceptId);
        acceptGoodsList = acceptGoodsMapper.selectList(wrapper);
        return ResponseData.buildOk(acceptGoodsList);
    }

    @Override
    public ResponseData removeGood(String id) {
        AcceptGoods acceptGoods = acceptGoodsMapper.selectById(id);
        QueryWrapper<AcceptInform> wrapper = new QueryWrapper<>();
        wrapper.eq("accept_id",acceptGoods.getAcceptId());
        AcceptInform acceptInform = acceptInformMapper.selectOne(wrapper);
        if(acceptInform.getStatus().equals("验收中")){
            int i = acceptGoodsMapper.deleteById(id);
            if (i > 0) return ResponseData.buildOk();
            return ResponseData.buildError();
        }else {
            return ResponseData.buildError();
        }


    }
}
